<?php
namespace app\serverauth\service\app;

use \app\manage\service\ApiBase;
/**
 * 对所用的应用的名字,key的管理
 * @author adv30 <adv30@126.com> 2017年6月1日9:26:23
 */
class App extends ApiBase
{
    protected $appValidate = '\app\serverauth\validate\App';
    protected $appLogic = '';
    // 增加一个app应用
    public function add(): array
    {
        $data = $this->haveIssetReceiveData(['name'=>' ', 'allow_role_ids'=>'']);
        return $this->oneStepRun($data, 'serveruser/auth/App/add', 'add');
    }
    // 禁用一个应用
    public function stop(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'app_key'=>'']);
        if(array_eq($data, '', ['id', 'name', 'app_key'])){
            return verify_err(null, '至少要传一个参数:id,name,app_key');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/stop', 'stop');
    }
    // 查看所有应用
    public function list(): array
    {
        $data = $this->haveIssetReceiveData(['page' => 1, 'list_rows' => '']);
        return $this->oneStepRun($data, 'serveruser/auth/App/list', 'list');
    }
    // 查看某一应用的信息
    public function find(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'','name'=>'','app_key'=>'']);
        if(array_eq($data, '')){
            return verify_err(null, '至少要传一个参数:id name app_key');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/find', 'find');
    }
    // 为一个应用增加可以访问的角色
    public function addRoles(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'','name'=>'', 'allow_role_ids'=>'']);
        if (array_eq($data, '', ['id', 'name'])) {
            return verify_err(null, '至少要传一个参数:id,name');
        } else if ($data['allow_role_ids'] === '') {
            return verify_err(null, 'allow_role_ids不可为空');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/addRoles', 'addRoles');
    }
    // 为一个应用增加可以访问的角色
    public function removeRoles(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'','name'=>'', 'allow_role_ids'=>'']);
        if (array_eq($data, '', ['id', 'name'])) {
            return verify_err(null, '至少要传一个参数:id,name');
        } else if ($data['allow_role_ids'] === '') {
            return verify_err(null, 'allow_role_ids不可为空');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/removeRoles', 'removeRoles');
    }
    // 启用某一应用的信息
    public function start(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'', 'name'=>'', 'app_key'=>'']);
        if(array_eq($data, '', ['id', 'name', 'app_key'])){
            return verify_err(null, '至少要传一个参数:id,name,app_key');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/start', 'start');
    }
    // 编辑某一应用的信息
    public function edit(): array
    {
        $data = $this->haveIssetReceiveData(['id'=>'0','name'=>'', 'allow_role_ids'=>'']);
        if(array_eq($data, '', ['name', 'allow_role_ids'])){
            return verify_err(null, '至少要传一个参数:name,allow_role_ids');
        }
        return $this->oneStepRun($data, 'serveruser/auth/App/edit', 'edit');
    }
    // 查看所有应用不分页
    public function all(): array
    {
        return $this->oneStepRun([], 'serveruser/auth/App/all', 'all');
    }
    // 根据app_ids获取app_name
    public function getAppNameByIds(): array
    {
        $data = $this->haveIssetReceiveData(['app_ids'=>'']);
        return $this->oneStepRun($data, 'serveruser/auth/App/getAppNameByIds', 'getAppNameByIds');
    }
}
